package com.is2.patchinglife.activity;

import com.facebook.Session;
import com.facebook.SessionState;
import com.is2.patchinglife.R;
import com.is2.patchinglife.manager.ActivityManager;
import com.is2.patchinglife.manager.LocalDataManager;
import com.is2.patchinglife.util.ExpandableListAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

public class TrophyActivity extends Activity {
 
    ExpandableListView expListView;
    ExpandableListAdapter expListAdapter;
    
    /** START FB **/
    public final Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };
    
    
    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {
            Log.d("TEST", "Logged in...");
        } else if (state.isClosed()) {
            Log.d("TEST", "Logged out...");
        }
    }
    /** END FB **/
 
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	
    	// draw activity
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trophy);

		// add activity to manager
		ActivityManager.getInstance().addActivity("TrophyActivity", this);
		        
        // associate Adapter to ExpandableListView
        expListView = (ExpandableListView) findViewById(R.id.trophyExpList);
        expListAdapter = new ExpandableListAdapter(this);
        expListView.setAdapter(expListAdapter);
        
        // set onExpandListener [sound + vibration]
        expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
        	@Override
            public void onGroupExpand(int groupPosition) {
        		if(LocalDataManager.getGotIT(expListAdapter.getTrophyKey(groupPosition))){
        			ActivityManager.executeButtonEffects();		// execute button effects
        		}
            }
        });
        
        // set onCollapseListener [sound + vibration]
        expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
            	if(LocalDataManager.getGotIT(expListAdapter.getTrophyKey(groupPosition))){
            		ActivityManager.executeButtonEffects();		// execute button effects		
            	}
            }
        });
	}
    
    
    @Override
    public void onDestroy(){
	 String[] aList = {"TrophyActivity"};
	 	ActivityManager.getInstance().closeActivities(aList,false); 
        super.onDestroy();
    }
    
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);    
    }
}